<?php
// +----------------------------------------------------------------------
// | Author: derek <693472824@qq.com> <http://www.bloveambition.com/>
// +----------------------------------------------------------------------

namespace Api\Controller;
use Think\Model;
use Api\Logic\UserLogic;

/**
 * 首页活动控制器
 * @author derek <693472824@qq.com>
 */
class ActivityController extends BaseController {
    
    /**
     * 首页商品列表
     * @author derek <693472824@qq.com>
     */
    public function viewAction(){
        
        $bannerID = I('id');
        
        $view = D('Blovex4/BannerView');
        $banner = $view->where(array('banner_id'=>$bannerID))->find();
        
        //活动正在进行时，判断当前用户是否已领取本次活动优惠券
        //无活动时，不需要登录
        if($banner['activity'] == '1'){
            
            //$this->checkLogin('Activity/viewAction', array('id'=>$bannerID));
            
            $userID = UserLogic::is_login();
            
            if($userID){
                
                $map['user_id'] = $userID;
                $map['code'] = $banner['random'];
                
                $coupon = M('UserCoupon')->where($map)->getField('coupon_id');
                $this->assign('coupon', $coupon);
                $this->assign('user_id', $userID);
            }else{
                $this->assign('user_id', $userID);
            }
        }
        
        $this->assign('banner', $banner);
        $this->display();
    }
    
    /**
     * 领取活动优惠券
     */
    public function receiveCoupon(){
        
        $random = I('random_num');
        $bannerID = I('rid');
        
        $userID = UserLogic::is_login();
        
        if(empty($userID)){
            $this->result['status'] = '-201';
        }else if(empty($random) || empty($bannerID)){
            $this->result['status'] = '-1';
        }else{
            
            $value = M('Banner')->find($bannerID);
            
            $userLogic = D('User', 'Logic');
            $result = $userLogic->coupon('3', $value['money'], $random);
            
            if($result){
                $this->result['status'] = '1';
            }else{
                $this->result['status'] = '0';
            }
        }
        
        $this->ajaxReturn($this->result);
    }
    
}
